package com.hh.bss.common.page;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import org.hibernate.util.StringHelper;
/**
 * 分页请求信息
 * 其中范型<T>为filters的类型
 * @author badqiu
 */
public class PageRequest<T> implements Serializable {

	/**
	 * 过滤参数
	 */
	@Deprecated
	private T filters;
	/**
	 * 页号码,页码从1开始
	 */
	private int pageNumber; //page 
	/**
	 * 分页大小
	 */
	private  int pageSize =10;  //默认十条 //rows
	
	private int page;
	private int rows;
	public int getRows() {
		return rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	/**
	 * 排序的多个列,如: username desc
	 */
	private String sortColumns;
	private String sort;
	private String dir;
	private String order;
	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.dir=order; //order 兼容dir 
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDir() {
		if (StringHelper.isEmpty(dir)) {
			System.out.println("dir null...................");
			return this.order;  //order 兼容dir 
		}else System.out.println("dir not null...................:"+dir);
			
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	private int start; 
	private int limit;
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public PageRequest() {
		//this(0,10);
	}
	
	@Deprecated
	public PageRequest(T filters) {
		this(0,10,filters);
	}
	
	public PageRequest(int pageNumber, int pageSize) {
		this(pageNumber,pageSize,(T)null);
	}
	
	@Deprecated
	public PageRequest(int pageNumber, int pageSize, T filters) {
		this(pageNumber,pageSize,filters,null);
	}
	
	public PageRequest(int pageNumber, int pageSize,String sortColumns) {
		this(pageNumber,pageSize,null,sortColumns);
	}
	
	@Deprecated
	public PageRequest(int pageNumber, int pageSize, T filters,String sortColumns) {
		this.pageNumber = pageNumber;
		this.pageSize = pageSize;
		setFilters(filters);
		setSortColumns(sortColumns);
	}
	
	@Deprecated
	public T getFilters() {
		return filters;
	}

	@Deprecated
	public void setFilters(T filters) {
		this.filters = filters;
	}

	public int getPageNumber() {
	    if (pageNumber==0&&limit!=0) pageNumber=(int)(start/limit)+1;
	    if (pageNumber==0&&page!=0) pageNumber=(int)page;
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public int getPageSize() {
		if (limit!=0) pageSize=limit;
		if (rows!=0) pageSize=rows;
		if (pageSize<=0) return 10; //默认十
		//System.out.println("pageSize:"+pageSize+"--"+limit);
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public String getSortColumns() {
		return sortColumns;
	}
	/**
	 * 排序的列,可以同时多列,使用逗号分隔,如 username desc,age asc
	 * @return
	 */
	public void setSortColumns(String sortColumns) {
		checkSortColumnsSqlInjection(sortColumns);
		if(sortColumns != null && sortColumns.length() > 50) {
			throw new IllegalArgumentException("sortColumns.length() <= 50 must be true");
		}
		this.sortColumns = sortColumns;
	}

	/**
	 * 将sortColumns进行解析以便返回SortInfo以便使用
	 * @return
	 */
	public List<SortInfo> getSortInfos() {
		return Collections.unmodifiableList(SortInfo.parseSortColumns(sortColumns));
	}
	
	private void checkSortColumnsSqlInjection(String sortColumns) {
		if(sortColumns == null) return;
		if(sortColumns.indexOf("'") >= 0 || sortColumns.indexOf("\\") >= 0) {
			throw new IllegalArgumentException("sortColumns:"+sortColumns+" has SQL Injection risk");
		}
	}
}
